"""
Case Type   : ?相关操作符
Case Name   : dolphin.b_compatibility_mode参数打开时，使用?相关操作符，合理报错
Create At   : 2024.8.19
Owner       : wang-tianjing1
Description :
    1、设置dolphin.b_compatibility_mode参数为on
    2、使用?相关操作符
    3、恢复dolphin.b_compatibility_mode参数
Expect      :
    1、成功
    2、失败，合理报错
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:设置dolphin.b_compatibility_mode参数为on;expect:成功----'
        self.log.info(text)
        sql_cmd = f'set dolphin.b_compatibility_mode to on;'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('SET', sql_res, '执行失败' + text)

        text = '----step2:使用?相关操作符;expect:失败，合理报错----'
        self.log.info(text)
        sql_cmd1 = f'''SELECT jsonb '{{{{\\"a\\":1, \\"b\\":2}}}}' ? 'b';'''
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("syntax error", sql_res1,'执行失败' + text)

        sql_cmd2 = f'''SELECT jsonb '{{{{\\"a\\":1, \\"b\\":2}}}}' ?| ARRAY['a','b'];'''
        self.log.info(sql_cmd2)
        sql_res2 = self.sh_primary.execut_db_sql(sql_cmd2)
        self.log.info(sql_res2)
        self.assertIn("syntax error", sql_res2, '执行失败' + text)

        sql_cmd3 = f'''SELECT jsonb '{{{{\\"a\\":1, \\"b\\":2}}}}' ?& ARRAY['a','b'];'''
        self.log.info(sql_cmd3)
        sql_res3 = self.sh_primary.execut_db_sql(sql_cmd3)
        self.log.info(sql_res3)
        self.assertIn("syntax error", sql_res3, '执行失败' + text)

        sql_cmd4 = f"SELECT lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))' AS RESULT;"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn("syntax error", sql_res4, '执行失败' + text)

        sql_cmd5 = f"SELECT ?- lseg '((-1,0),(1,0))' AS RESULT;"
        self.log.info(sql_cmd5)
        sql_res5 = self.sh_primary.execut_db_sql(sql_cmd5)
        self.log.info(sql_res5)
        self.assertIn("syntax error", sql_res5, '执行失败' + text)

        sql_cmd6 = f"SELECT point '(1,0)' ?- point '(0,0)' AS RESULT;"
        self.log.info(sql_cmd6)
        sql_res6 = self.sh_primary.execut_db_sql(sql_cmd6)
        self.log.info(sql_res6)
        self.assertIn("syntax error", sql_res6, '执行失败' + text)

        sql_cmd7 = f"SELECT ?| lseg '((-1,0),(1,0))' AS RESULT;"
        self.log.info(sql_cmd7)
        sql_res7 = self.sh_primary.execut_db_sql(sql_cmd7)
        self.log.info(sql_res7)
        self.assertIn("syntax error", sql_res7, '执行失败' + text)

        sql_cmd8 = f"SELECT lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))' AS RESULT;"
        self.log.info(sql_cmd8)
        sql_res8 = self.sh_primary.execut_db_sql(sql_cmd8)
        self.log.info(sql_res8)
        self.assertIn("syntax error", sql_res8, '执行失败' + text)

        sql_cmd9 = f"SELECT lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))' AS RESULT;"
        self.log.info(sql_cmd9)
        sql_res9 = self.sh_primary.execut_db_sql(sql_cmd9)
        self.log.info(sql_res9)
        self.assertIn("syntax error", sql_res9, '执行失败' + text)


    def tearDown(self):
        text = '--step3:清理环境，恢复参数;expect:成功--'
        self.log.info(text)
        sql_cmd = f"reset dolphin.b_compatibility_mode;"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn("RESET", sql_res, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
